package com.newtouch.bxzs.business.personal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.personal.mapper.ScoreMapper;
import com.newtouch.bxzs.business.personal.model.ScoreVO;
import com.newtouch.bxzs.business.personal.service.ScoreService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.Score;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * APP评分服务实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年1月5日</td>
 * <td>xiongdianqiang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author xiongdianqiang
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class ScoreServiceImpl implements ScoreService {
	@Autowired
	private ScoreMapper scoreMapper;

	@Autowired
	private PersonalMapper personalMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.newtouch.bxzs.business.personal.service.ScoreService#addEvaluateScore
	 * (java. lang.Integer)
	 */
	@Override
	public String addEvaluateScore(ScoreVO scoreVO) {
		if (scoreVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		// 根据id查询用户信息
		User user = personalMapper.selectById(userId);
		if (BeanUtil.isEmpty(user)) {
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		if (scoreVO.getAppScoreValue() > 5 || scoreVO.getAppScoreValue() < 1) {
			throw new BaseException("评分为1-5,请重新评价！");
		}
		Score score = new Score();
		score.setCreateUserId(userId);
		score.setCreateTime(new Date());
		score.setUpdateUserId(userId);
		score.setUpdateTime(new Date());
		score.setAppScoreValue(scoreVO.getAppScoreValue());
		scoreMapper.insert(score);
		return score.getAppScoreId();
	}
}
